package com.smartservice.ui;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import com.example.muachung.R;
import com.smartservice.conf.constant;

@SuppressLint("NewApi")
public class TabFragmentSeason extends ListFragment {
//	ArrayList<Episode> eposides;
	String list_ep[];
	//ArrayList<Episode> episodes = new ArrayList<Episode>();
	Bundle bundle;
	LayoutInflater mInflater;
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Bundle bundle = msg.getData();
			if (bundle.containsKey(constant.BD_JSON_KEY_CONTENT_OF_SERIAL)) {
				try {
					JSONObject jsonObj = new JSONObject(
							bundle.getString(constant.BD_JSON_KEY_CONTENT_OF_SERIAL));
					jsonObj = jsonObj
							.getJSONObject(constant.TAG_GET_LIST_CONTENT_OF_GROUP_ID_RESAULT);
					JSONArray contents = jsonObj
							.getJSONArray(constant.TAG_LIST_CONTENT);
					String content_name[] = new String[contents.length()];
					for (int i = 0; i < contents.length(); i++) {
						JSONObject jsonObject = contents.getJSONObject(i);
//					Episode ep = new Episode();
//						ep.setGroupID(jsonObject
//								.getString(constant.TAG_CONTENT_GROUP_ID_1));
//						ep.setContentID(jsonObject
//								.getString(constant.TAG_CONTENT_ID));
//						ep.setDescr(jsonObject
//								.getString(constant.TAG_CONTENT_DESC));
//						ep.setContentMediaUrl(jsonObject
//								.getString(constant.TAG_CONTENT_MEDIA_URL));
//						ep.setContentDocUrl(jsonObject
//								.getString(constant.TAG_CONTENT_DOC_URL));
//						episodes.add(ep);
						content_name[i] = jsonObject
								.getString(constant.TAG_CONTENT_NAME);
					}
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(
							mInflater.getContext(), R.layout.lv_item_episode,
							R.id.text, content_name);
					setListAdapter(adapter);
				} catch (JSONException e) {
					Log.e("ERR", "cannot read json" + e.toString());
				}
			}// end if
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.mInflater = inflater;
		if (container == null) {
			return null;
		}
		bundle = getArguments();
		if (bundle != null) {

			String id = bundle.getString(constant.BD_ID_FRAGMENT);
//			new JSONParserTask(constant.URL_GET_LIST_CONTENT_OF_GROUP_ID + "/"
//					+ id, constant.BD_JSON_KEY_CONTENT_OF_SERIAL, mHandler)
//					.execute();

		}
		return (LinearLayout) inflater.inflate(R.layout.tab_frag_serial,
				container, false);
	}
}
